package cn.bdqn.controller;

import cn.bdqn.pojo.Customer;
import cn.bdqn.pojo.Orders;
import cn.bdqn.pojo.OrdersLine;
import cn.bdqn.service.CustomerService;
import cn.bdqn.service.OrdersLineService;
import cn.bdqn.service.OrdersService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrdersService ordersService;
    @Resource
    private CustomerService customerService;
    @Resource
    private OrdersLineService ordersLineService;
    /**
     * 历史订单分页查询
     * @param custNo
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String custNo, @RequestParam(defaultValue = "1") Integer pageIndex, Model model) {
        //设置每页显示条数
        int pageSize = 3;
        //查询每页显示的数据
        IPage<Orders> ordersPager= ordersService.getOrdersByPage(custNo, pageIndex, pageSize);
        System.out.println(ordersPager);
        model.addAttribute("ordersPager", ordersPager);
        //查询条件回显
        model.addAttribute("custNo", custNo);
        //查询客户信息
        Customer customer=customerService.getCustomer(custNo);
        model.addAttribute("customer",customer);
        return "order/list";
    }
    /**
     * 查看订单明细
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/view")
    public String view(Long id,Model model){
    //根据订单编号，查询订单信息
        Orders orders=ordersService.getOrders(id);
        model.addAttribute("orders",orders);
    //根据订单编号，查询订单明细
        List<OrdersLine> ordersLineList=
                ordersLineService.getOrdersLineByOrderId(id);
        model.addAttribute("ordersLineList",ordersLineList);
        return "order/view";
    }


}